package org.bluedream.core.module.sys.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.bluedream.comm.base.BaseServiceImpl;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.comm.utils.EncryForRSAUtil;
import org.bluedream.core.module.sys.dao.SysLicenseDao;
import org.bluedream.core.module.sys.entity.SysLicense;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * @ClassName SysLicenseService
 * @Description TODO
 * @Author Administrator
 * @Data 2022/7/27 9:02
 * @Version
 */
@Service
@DS("master")
@Transactional(
        readOnly = true
)
public class SysLicenseService extends BaseServiceImpl<SysLicenseDao, SysLicense> implements IService<SysLicense> {
    private static final Logger logger = LoggerFactory.getLogger(SysLicenseService.class);

    public boolean check(){
        List<SysLicense> list = super.list();
        if (EmptyUtil.isEmpty(list) || list.size() != 1){
            return false;
        }
        SysLicense s0 = list.get(0);
        String str0;
        try {
            str0 = EncryForRSAUtil.decryByByRSAKey(s0.getCert() , s0.getKey() , EncryForRSAUtil.PRIVATE_KEY);
        }catch (Exception e1){
            logger.error("授权信息解密失败！");
            return false;
        }
        SysLicense s1 = JSON.parseObject(str0, SysLicense.class);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int d0 = sdf.format(date).compareTo(s1.getSystemDate());
        if (s1.getSystemDate().equals(s0.getSystemDate()) && d0 < 0){
            return true;
        }
        return false;
    }

}